#include "pcl/io/pcd_io.h"/******************************************************************************
 * Copyright 2022 The Untouch Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#pragma once

#include <memory>

#include <glk/pointcloud_buffer.hpp>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

#include "modules/localization/msf/common/io/pcl_point_types.h"

namespace lidar_imu_calibrator {

using namespace apollo::localization::msf;

class OdometryFrame {
 public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  using Ptr = std::shared_ptr<OdometryFrame>;

 public:
  OdometryFrame(const std::string& raw_cloud_path,
                const Eigen::Isometry3d& pose, double timestamp);
  double GetFrameTimestamp();
  const Eigen::Isometry3d& GetFramePose();
  const Eigen::Isometry3d& GetImuFramePose();
  void SetFrameImuPose(const Eigen::Isometry3d& pose);
  void UpdateFramePose(const Eigen::Isometry3d& extrinsics);
  void LoadPointCloud(
      const std::vector<double>& timestamps,
      std::vector<Eigen::Isometry3d,
                  Eigen::aligned_allocator<Eigen::Isometry3d>>& poses,
      Eigen::Isometry3d& extrinsic);
  void GetPointCloud();
  void GetPointCloud(pcl::PointCloud<pcl::PointXYZI>::Ptr &point_cloud);
  void DrawPointCloud(glk::GLSLShader& shader,
                      float downsample_resolution = 0.2f);

 private:
  void GetTimestampInterval(
      pcl::PointCloud<velodyne::PointXYZIT>::Ptr& input_point_cloud,
      double& timestamp_min, double& timestamp_max);
  size_t GetIndex(const std::vector<double>& timestamps, double timestamp);
  void ComputeInterpolationPose(
      double timestamp, const std::vector<double>& timestamps,
      std::vector<Eigen::Isometry3d,
                  Eigen::aligned_allocator<Eigen::Isometry3d>>& in_poses,
      Eigen::Isometry3d& extrinsic, Eigen::Isometry3d& out_pose);
  void MotionCompensation(
      pcl::PointCloud<velodyne::PointXYZIT>::Ptr& input_point_cloud,
      double timestamp_min, double timestamp_max,
      const Eigen::Isometry3d& pose_min_time,
      const Eigen::Isometry3d& pose_max_time,
      pcl::PointCloud<pcl::PointXYZI>::Ptr& output_point_cloud);

 private:
  bool point_cloud_buffer_need_reset_;
  double timestamp_;
  Eigen::Isometry3d pose_;
  Eigen::Isometry3d pose_imu_;
  std::string raw_cloud_path_;
  float downsample_resolution_;
  pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud_;
  std::unique_ptr<glk::PointCloudBuffer> point_cloud_buffer_;
};
}  // namespace lidar_imu_calibrator